package com.sendi.system.bean;

import java.io.Serializable;
 
import java.util.Date;
import java.util.Iterator;

import org.hibernate.CallbackException;
import org.hibernate.EntityMode;
import org.hibernate.Interceptor;
import org.hibernate.Transaction;
import org.hibernate.type.Type;

import com.sendi.system.service.ServiceSupportImp;
 
 
  
  
public class SysLogInterceptor implements Interceptor{   
  
	public ApplicationContextHelper applicationContextHelper;
    public LoginInfo loginInfo;
    public SysLogHelper sysLogHelper;
   public SysLogbean sysLogbean;
     //删除时记录时间   
    public void onDelete(Object obj, Serializable arg1, Object[] arg2, String[] arg3, Type[] arg4) throws CallbackException {   
     if(loginInfo==null)
     {
    	 loginInfo=(LoginInfo) applicationContextHelper.appctx.getBean("loginInfo");
    	 
     }
    if(sysLogbean==null)
    {
    	sysLogbean=(SysLogbean)applicationContextHelper.appctx.getBean("sysLogbean");
    }
    String beanname=obj.getClass().getName();
 	 if(sysLogbean.isNeedlog(beanname))
 	 {
 		 sysLogHelper.Log(loginInfo.userid, loginInfo.loginhost,"sucess", new Date().toLocaleString(), "delete :"+beanname+" 业务相关:"+sysLogbean.getBeanBsName(beanname), "delete");
 	 }
     
    }   
                //修改时记录事件   
     public boolean onFlushDirty(Object obj, Serializable id, Object[] currentState, Object[] previousState , String[] propertyNames, Type[] types){   
    	 if(loginInfo==null)
	     {
	    	 loginInfo=(LoginInfo) applicationContextHelper.appctx.getBean("loginInfo");
	     }
    	 if(sysLogbean==null)
    	    {
    	    	sysLogbean=(SysLogbean)applicationContextHelper.appctx.getBean("sysLogbean");
    	    }
    	 String beanname=obj.getClass().getName();
    	 if(sysLogbean.isNeedlog(beanname))
    	 {
    		 sysLogHelper.Log(loginInfo.userid, loginInfo.loginhost,"sucess", new Date().toLocaleString(), "modify :"+beanname+" 业务相关:"+sysLogbean.getBeanBsName(beanname), "modify");
    	 }
      	
         return false;   
    }   
    public String onPrepareStatement(String arg0) {   
       
        return arg0;   
    }   
               //保存时记录事件   
    public boolean onSave(Object obj, Serializable arg1, Object[] arg2, String[] arg3, Type[] arg4) throws CallbackException {   
    	   if(loginInfo==null)
    	     {
    	    	 loginInfo=(LoginInfo) applicationContextHelper.appctx.getBean("loginInfo");
    	     }
    	   if(sysLogbean==null)
    	    {
    	    	sysLogbean=(SysLogbean)applicationContextHelper.appctx.getBean("sysLogbean");
    	    }
    	   String beanname=obj.getClass().getName();
      	 if(sysLogbean.isNeedlog(beanname))
      	 {
      		 sysLogHelper.Log(loginInfo.userid, loginInfo.loginhost,"sucess", new Date().toLocaleString(), "save :"+beanname+" 业务相关:"+sysLogbean.getBeanBsName(beanname), "save");
      	 }
    	  
       	
    	return false;   
    }   
               
 
	public void afterTransactionBegin(Transaction arg0) {
		// TODO Auto-generated method stub
		
	}
	public void afterTransactionCompletion(Transaction arg0) {
		// TODO Auto-generated method stub
		
	}
	public void beforeTransactionCompletion(Transaction arg0) {
		// TODO Auto-generated method stub
		
	}
	public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2,
			Object[] arg3, String[] arg4, Type[] arg5) {
		// TODO Auto-generated method stub
		return null;
	}
	public Object getEntity(String arg0, Serializable arg1)
			throws CallbackException {
		// TODO Auto-generated method stub
		return null;
	}
	public String getEntityName(Object arg0) throws CallbackException {
		// TODO Auto-generated method stub
		return null;
	}
	public Object instantiate(String arg0, EntityMode arg1, Serializable arg2)
			throws CallbackException {
		// TODO Auto-generated method stub
		return null;
	}
	public Boolean isTransient(Object arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	public void onCollectionRecreate(Object arg0, Serializable arg1)
			throws CallbackException {
		// TODO Auto-generated method stub
		
	}
	public void onCollectionRemove(Object arg0, Serializable arg1)
			throws CallbackException {
		// TODO Auto-generated method stub
		
	}
	public void onCollectionUpdate(Object arg0, Serializable arg1)
			throws CallbackException {
		// TODO Auto-generated method stub
		
	}
	public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2,
			String[] arg3, Type[] arg4) throws CallbackException {
		// TODO Auto-generated method stub
		return false;
	}
	public void postFlush(Iterator arg0) throws CallbackException {
		// TODO Auto-generated method stub
		
	}
	public void preFlush(Iterator arg0) throws CallbackException {
		// TODO Auto-generated method stub
	}
	public LoginInfo getLoginInfo() {
		return loginInfo;
	}
	public void setLoginInfo(LoginInfo loginInfo) {
		this.loginInfo = loginInfo;
	}
	public ApplicationContextHelper getApplicationContextHelper() {
		return applicationContextHelper;
	}
	public void setApplicationContextHelper(
			ApplicationContextHelper applicationContextHelper) {
		this.applicationContextHelper = applicationContextHelper;
	}
	public SysLogHelper getSysLogHelper() {
		return sysLogHelper;
	}
	public void setSysLogHelper(SysLogHelper sysLogHelper) {
		this.sysLogHelper = sysLogHelper;
	}
	public SysLogbean getSysLogbean() {
		return sysLogbean;
	}
	public void setSysLogbean(SysLogbean sysLogbean) {
		this.sysLogbean = sysLogbean;
	}
		
	}  
